#!/bin/bash

################################################################################
# This bash script is written for run dark energy equation (EoS) reconstructions
# from an ensemble of mock samples (SN + CMB distance prior).
#
#   Youhua Xu
#   @2017-11-19
################################################################################

# total number of mock samples
NumMockTot=100

# counter (start from 1)
n=1

# current work directory
PWD=`pwd`

# CLASS_INI directory
DIR_CLASSMC_INI="$PWD/classmc_ini"

if [ -d $DIR_CLASSMC_INI ]
then
    echo "folder $DIR_CLASSMC_INI already exist"
else
    echo "folder $DIR_CLASSMC_INI does not exist, so I will create it"
#    echo "mkdir $DIR_CLASSMC_INI ..."
    mkdir $DIR_CLASSMC_INI
    touch $DIR_CLASSMC_INI"/.base"
fi

# JOB script directory
DIR_JOBS="$PWD/jobs"

if [ -d $DIR_JOBS ]
then
    echo "folder $DIR_JOBS already exist"
else
    echo "folder $DIR_JOBS does not exist, so I will create it"
#    echo "mkdir $DIR_JOBS ..."
    mkdir $DIR_JOBS
    touch $DIR_JOBS"/.base"
fi

# LOGFILE directory
DIR_LOGS="$PWD/logs"

if [ -d $DIR_LOGS ]
then
    echo "folder $DIR_LOGS already exist"
else
    echo "folder $DIR_LOGS does not exist, so I will create it"
#    echo "mkdir $DIR_LOGS ..."
    mkdir $DIR_LOGS
    touch $DIR_LOGS"/.base"
fi

# some template *ini files
TEMP_CLASSMC_INI="template_classmc_eos.ini"
TEMP_SNE_INI_1="jla_mock_dataset.ini"
TEMP_SNE_INI_2="wfirst_mock_dataset.ini"
TEMP_CMB_INI="CMB_dist_prior_dataset.ini"
TEMP_DDE_INI="DDE_CPZ_prior.ini"
TEMP_JOB="template_job"

# some key parameters in the input *ini file
CHAIN_ROOT=""
KEY_OUT_ROOT=""
KEY_SNE_INI=""
KEY_CMB_INI=""
KEY_DDE_INI=""

# some key parameters in the job script
KEY_JOB_ID=""
KEY_LOGFILE=""
KEY_INI=""


while [ $n -le $NumMockTot ]
do
# create chain folder for n-th reconstruction
CHAIN_DIR="$PWD/chain_$n"
if [ -d $CHAIN_DIR ]
then
    echo "folder $CHAIN_DIR already exist"
else
    echo "folder $CHAIN_DIR does not exist, so I will create it"
#    echo "mkdir $CHAIN_DIR ..."
    mkdir $CHAIN_DIR
fi

# create input parameter files (*.ini) and place them in chain_n
# 1) update the output chain root
# 2) update SN dataset file
# 3) update CMB dataset file (actually not necessary)
# 4) update DDE dataset file (actually not necessary)

CURRENT_CLASSMC_INI="$DIR_CLASSMC_INI/EoS_SN_CMB_$n.ini"

CURRENT_SNE_INI_1="$CHAIN_DIR/JLA_dataset.ini"
CURRENT_SNE_INI_2="$CHAIN_DIR/WFIRST_dataset.ini"

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# if use other MOCK SNe data, change the following two lines !!!

# --> for MOCK JLA
m4  -DKEY_MOCK="data_file = $PWD/mock_JLA/MOCK_JLA_$n.txt" \
    -DKEY_INVCOVMAT="icov_file = $PWD/jla_covmat_inv.txt" \
    $TEMP_SNE_INI_1 > $CURRENT_SNE_INI_1

# --> for MOCK WFIRST
m4	-DKEY_MOCK="data_file = $PWD/mock_WFIRST/WFIRST_SN_$n.txt" \
	$TEMP_SNE_INI_2 > $CURRENT_SNE_INI_2

#cat $CURRENT_SNE_INI  # for debug

CURRENT_CMB_INI="$CHAIN_DIR/CMB_dataset.ini"
CURRENT_DDE_INI="$CHAIN_DIR/DDE_dataset.ini"

m4  -DKEY_DIST_FILE="dist_prior_file = $PWD/CMB_dist_prior/mock_wmap9_dist_prior.txt" \
    -DKEY_DIST_FILE_HU="dist_prior_file_Hu = $PWD/CMB_dist_prior/mock_wmap9_dist_prior_Hu.txt" \
    -DKEY_COVMAT_FILE="inv_covmat_file = $PWD/CMB_dist_prior/wmap9_dist_prior_inv_covmat.txt" \
    $TEMP_CMB_INI > $CURRENT_CMB_INI

#cat $CURRENT_CMB_INI # for debug

cp $TEMP_DDE_INI $CURRENT_DDE_INI

CHAIN_ROOT="$CHAIN_DIR/EoS_SN_CMB"

KEY_OUT_ROOT="chain_root = $CHAIN_ROOT"
KEY_SNE_INI_1="JLA_Mock_dataset = $CURRENT_SNE_INI_1"
KEY_SNE_INI_2="WFIRST_dataset = $CURRENT_SNE_INI_2"
KEY_CMB_INI="CMB_distance_prior_dataset = $CURRENT_CMB_INI"
KEY_DDE_INI="DDE_CPZ_prior_settings = $CURRENT_DDE_INI"

m4  -DKEY_OUT_ROOT="$KEY_OUT_ROOT" \
    -DKEY_SNE_INI_1="$KEY_SNE_INI_1" \
	-DKEY_SNE_INI_2="$KEY_SNE_INI_2" \
    -DKEY_CMB_INI="$KEY_CMB_INI" \
    -DKEY_DDE_INI="$KEY_DDE_INI" \
    $TEMP_CLASSMC_INI > $CURRENT_CLASSMC_INI

# create job script:
# 1) update KEY_JOB_ID
# 2) update KEY_LOGFILE
# 3) update KEY_INI

CURRENT_JOB="$DIR_JOBS/EoS_SN_CMB_JOB.$n"

python gen_job.py "EoS_$n" "$DIR_LOGS/EoS_SN_CMB_LOG_$n" "$CURRENT_CLASSMC_INI" temp_job
mv temp_job $CURRENT_JOB

# Now submit the job and wait for the results
qsub -q x120.q -pe mpich 12 $CURRENT_JOB

# update counter
n=`expr 1 + $n`

sleep 5
done
